<?php
namespace app\widget\controller;

use DataWidget\entity\ActionLink;
use DataWidget\widget\ListWidget;
use DataWidget\entity\PageLink;
use DataWidget\widget\FormWidget;
use DataWidget\widget\FilterWidget;
use DataWidget\utility\QueryFilterHandler;
use think\Db;

class DbRecordController extends RecordController
{
    public function index()
    {
        // if(!$data=cache("record")){
        //     $data=$this->data;
        //     cache("record",$data);
        // }
        // $handler=new ArrayFilterHandler($data);
        $handler = QueryFilterHandler::decorate(Db::name("record"));
        if(!$handler->count()){
            $handler->insert($this->data[0]);
        }
        
        $map=include __DIR__ . "/../view/base/_map.php";
        $filter=FilterWidget::alloc()->setMap($map["filter"])->setHandler($handler)->setInputs($this->filters)->input(input());
        $list=ListWidget::alloc()->setMap($map["field"])->loadFields($this->fields)

        //->setData($handler->data)
        ->setData($handler->select())

        ->addAction("修改",function($item){
            return url("edit",["id"=>$item["id"]]);
        })
        ->setItemActions(function($item){
            return [
                ActionLink::create("复制",url("copy",["id"=>$item["id"]])),
                ActionLink::create("删除",url("delete",["id"=>$item["id"]]),"确认删除这条数据?")
            ];
        })
        ->addFlag("selectable");

        //绑定模板和数据
        return $this->fetch('',[
            "tabs"=>PageLink::fillList([
                ["title"=>"列表","href"=>url("index"),"active"=>true],
                ["title"=>"新建","href"=>url("add")],
            ]),
            "list"=>$list->toArray(),
            "entire_actions"=>[ActionLink::build("重置",url('reset'),"确认重置数据?","数据将被重置为默认数据")],
            "multi_actions"=>ActionLink::fillList([
                ["title"=>"删除","href"=>url("delete"),"confirm"=>"确认删除选中的数据?"],
            ]),
            "filter"=>$filter->toArray(),
            "filter_actions"=>ActionLink::fillList([
                ["title"=>"删除","href"=>url("delete"),"confirm"=>"确认删除所有匹配的数据?"],
            ]),
        ]);
    }
    public function reset()
    {
        $query = Db::name("record");
        $query->where("1=1")->delete();
        $query->insert($this->data[0]);
        $this->redirect('index');
    }
    public function add()
    {
        $map=include __DIR__ . "/../view/base/_map.php";
        $form=FormWidget::alloc()->setMap($map["field"])->setInputs($this->fields)->setAction(url('insert'),"提交");
        return $this->fetch('edit',['form'=>$form->toArray()]);
    }
    public function copy()
    {
        if(!$id=input("id")) $this->error('ID不能为空');
        if(!$record = Db::name("record")->where(["id"=>$id])->find()) $this->error('指定数据不存在');

        $map=include __DIR__ . "/../view/base/_map.php";
        $form=FormWidget::alloc()->setMap($map["field"])->setInputs($this->fields)->setValues($record)->setAction(url('insert'),"提交");
        return $this->fetch('edit',['form'=>$form->toArray()]);
    }
    public function edit()
    {
        if(!$id=input("id")) $this->error('ID不能为空');
        if(!$record = Db::name("record")->where(["id"=>$id])->find()) $this->error('指定数据不存在');

        $map=include __DIR__ . "/../view/base/_map.php";
        $form=FormWidget::alloc()->setMap($map["field"])->setInputs($this->fields)->setValues($record)->setAction(url('update'),"提交")->toArray();

        return $this->fetch('edit',[
            'form'=>$form,
        ]);
    }
    public function update()
    {
        $map=include __DIR__ . "/../view/base/_map.php";
        $data=FormWidget::alloc()->setMap($map["field"])->setInputs($this->fields)->getInput(input());
        $id = $data["id"]; unset($data["id"]);
        Db::name("record")->where(["id"=>$id])->update($data);
        $this->redirect('index');
    }
    public function insert()
    {
        $map=include __DIR__ . "/../view/base/_map.php";
        $data=FormWidget::alloc()->setMap($map["field"])->setInputs($this->fields)->getInput(input());
        unset($data["id"]);
        Db::name("record")->insert($data);
        $this->redirect('index');
    }
    public function delete()
    {
        $data=input();
        if(isset($data["id"])){
            Db::name("record")->where(["id"=>$data["id"]])->delete();
        }
        if(isset($data["select"])){
            Db::name("record")->whereIn("id",$data["select"])->delete();
        }
        $this->redirect('index');
    }
}
